package pattern.state.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pattern.state.model.Car;

public class MainFrame extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5581154746911217844L;

	private JButton accelerate = new JButton("accelerate");
	private JButton slowDown = new JButton("slow down");
	private JButton stop = new JButton("stop");
	private JButton goBack = new JButton("go back");
	private JTextField info = new JTextField(30);
	private JPanel jplPanel = new JPanel();
	
	private Car car = null;
	
	public MainFrame(final Car car){
		this.car = car;
		this.setVisible(true);
		this.setSize(400, 100);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		accelerate.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				
				try {
					car.getState().accelerate();
					info.setText(car.getText());
				} catch (Exception ie) {
					info.setText(ie.getMessage());
				}
			}
		});
		
		slowDown.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				
				try {
					car.getState().slowDown();
					info.setText(car.getText());
				} catch (Exception ie) {
					info.setText(ie.getMessage());
				}
			}
		});
		
		stop.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				
				try {
					car.getState().stop();
					info.setText(car.getText());
				} catch (Exception ie) {
					info.setText(ie.getMessage());
				}
			}
		});
		
		goBack.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				
				try {
					car.getState().goBack();
					info.setText(car.getText());
				} catch (Exception ie) {
					info.setText(ie.getMessage());
				}
			}
		});
		
		
		
		jplPanel.setLayout(new FlowLayout());
		jplPanel.add(info);
		jplPanel.add(accelerate);
		jplPanel.add(slowDown);
		jplPanel.add(goBack);
		jplPanel.add(stop);
		
		this.add(jplPanel);
		
		
	}
	
	

}
